/**
 * Created with IntelliJ IDEA.
 * User: Администратор
 * Date: 18.08.12
 * Time: 11:22
 * To change this template use File | Settings | File Templates.
 */
define(["dojo/_base/declare", "dijit/_WidgetBase", "dijit/_TemplatedMixin",
        "dojo/text!./LedClock/templates/LedClock.html", "dojo/dom-style", "dojo/_base/fx", "dojo/_base/lang", "dojo"],
    function (declare, WidgetBase, TemplatedMixin, template, domStyle, baseFx, lang, dojo) {
        return declare([WidgetBase, TemplatedMixin], {

            currentValue : '000000',

            // Our template - important!
            templateString:template,

            // A class to be applied to the root node in our template
            baseClass:"ledClock",

            constructor: function(){
                this._set("currentValue", this._getValue());
            },

            postCreate : function () {
                this.digits = dojo.query('.digit', this.containerNode);
                this._render();
                setInterval(dojo.hitch(this, function() {this._setValue(this._getValue());}), 1000);
            },

            _render : function () {
                var i = 0;
                var dc = Math.min(this.digits.length, this.currentValue.length);
                this.digits.removeClass();
                for (i=0; i < dc; i++) {
                    dojo.addClass(this.digits[i],'digit_'+this.currentValue.charAt(i));
                }
            },

            _setValue : function (value) {
                this.currentValue = value;
                this._render();
            },

            _getValue: function() {
                var currentTime = new Date();
                var h = currentTime.getHours();
                var m = currentTime.getMinutes();
                var s = currentTime.getSeconds();
                if (h<=9) h="0"+h
                if (m<=9) m="0"+m
                if (s<=9) s="0"+s;
                return h.toString()+m.toString()+s.toString();
            }
        });
    });